// 基础知识
// 1. typeof
// 获取类型结构
interface Person {
    name: string;
    age: number;
}
const tom: Person = {
    name: 'tom',
    age: 18
}
type Tom = typeof tom
const jerry: Tom = {
    name: 'jerry',
    age: 18
}
// 获取函数对象的类型
function toArray(x: number): number[] {
    return [x]
}
type Func = typeof toArray
let myToArray: Func = (x) => [x]
// 2.keyof
type k1 = keyof Person //  // "name" | "age"
// mike: 类型必须符合k1, 类型、属性缺一不可
let mike: Person = {
    name: 'mike',
    age: 10
}
// 应用
function myProp(obj: object, key: string) {
    return (obj as any)[key];
}
function prop<T extends object, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
myProp(mike, "a")
// prop(mike, "a") // 类型“"a"”的参数不能赋给类型“keyof Person”的参数
// in：用来遍历枚举类型
type keys = 'name' | 'count'
type Animal = {
    [p in keys]: any
}
/* type Animal = {
    name: any;
    count: any;
} */
let pig: Animal = {
    name: 'pig',
    count: 111
}
// infer
/* type ReturnType<T> = T extends (
  ...args: any[]
) => infer R ? R : any; */
type ParamsType<T extends (...args: any) => any> = T extends (...args: infer R) => any ? R : any;
const add = (a: number, b: string) => a + b
type TypeAdd = typeof add // (a: number, b: string) => string
type T1 = ParamsType<TypeAdd> // [number, string]
// const arr: T1 = [1, 2] // Error: Type 'number' is not assignable to type 'string'.

// extends
interface Lengthwise {
    length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
    console.log(arg.length);
    return arg;
}
// loggingIdentity(3)
loggingIdentity({length: 10, value:3 })
  